Partial Fraction Expansion using Sympy

This is an example for using partial fraction expansion within Python

This only covers a tiny fraction of what is possible.

As always it's a good idea to look at the documentation

http://docs.sympy.org/latest/index.html


In [1]:
import sympy 
import numpy as np
sympy.init_printing()

Lets try to complete the examples in section 4-3 using Sympy instead of Matlab

Consider the transfer function:

$$ \frac{Y(s)}{U(s)} = \frac{s^4 + 8 s^3 + 16 s^2 + 9s + 6}{s^3 + 6s^2+11s +6} $$

We want to use partial fraction expansion to simplify this expression

First we need to define the function in Python


In [5]:
s = sympy.symbols('s')

G = (s**4 + 8*s**3+16*s**2+9*s+6) / (s**3 + 6*s**2 + 11*s +6)
G


Out[5]:
$$\frac{s^{4} + 8 s^{3} + 16 s^{2} + 9 s + 6}{s^{3} + 6 s^{2} + 11 s + 6}$$

Now we can expand using partial fractions


In [6]:
sympy.apart(G)


Out[6]:
$$s + 2 - \frac{6}{s + 3} - \frac{4}{s + 2} + \frac{3}{s + 1}$$

Complex roots

Here's another example with complex roots to see what happens

$$ Y(s) = U(s) G(s) = \frac{1}{s} \frac{2s+10}{s^2 +2s+10} $$

In [10]:
F = 1 / s * (2*s+10)/(s**2+2*s+10)
F


Out[10]:
$$\frac{2 s + 10}{s \left(s^{2} + 2 s + 10\right)}$$

In [13]:
sympy.apart(F, full=True).doit()


Out[13]:
$$\frac{\frac{67}{90} - \frac{23 i}{30} + \frac{7}{45} \left(-1 - 3 i\right)^{2}}{s + 1 + 3 i} + \frac{\frac{67}{90} + \frac{7}{45} \left(-1 + 3 i\right)^{2} + \frac{23 i}{30}}{s + 1 - 3 i} + \frac{1}{s}$$